package com.xiaoer.studyenglishdemo.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.PickVisualMediaRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.xiaoer.studyenglishdemo.R;
import com.xiaoer.studyenglishdemo.bean.WordBean;
import com.xiaoer.studyenglishdemo.util.WordsDBDao;

import java.io.File;
import java.util.List;

public class EditWordsActivity extends AppCompatActivity {

    private int id;
    ImageView iv_back;
    ImageView iv_img;
    Button btn_update_pic;
    TextView tv_local_pic;
    EditText edt_pic;
    EditText edt_introduce;
    EditText edt_means;
    EditText edt_short_sentence;
    EditText edt_sentence1;
    EditText edt_sentence2;
    EditText edt_sentence3;
    Button btn_commit;
    RadioGroup rg_word_level, rg_study_type;
    LinearLayout ll_btn8;   // 学习分类
    private ActivityResultLauncher<PickVisualMediaRequest> pickMedia;
    private WordBean wordBean;

    public static void startMe(Context context, int id) {
        Intent intent = new Intent(context, EditWordsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        id = getIntent().getIntExtra("id", -1);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_words);

        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v -> finish());
        iv_img = findViewById(R.id.iv_img);
        btn_update_pic = findViewById(R.id.btn_update_pic);
        tv_local_pic = findViewById(R.id.tv_local_pic);
        edt_pic = findViewById(R.id.edt_pic);
        edt_introduce = findViewById(R.id.edt_introduce);
        edt_means = findViewById(R.id.edt_means);
        edt_short_sentence = findViewById(R.id.edt_short_sentence);
        edt_sentence1 = findViewById(R.id.edt_sentence1);
        edt_sentence2 = findViewById(R.id.edt_sentence2);
        edt_sentence3 = findViewById(R.id.edt_sentence3);
        btn_commit = findViewById(R.id.btn_commit);
        rg_word_level = findViewById(R.id.rg_word_level);
        rg_study_type = findViewById(R.id.rg_study_type);
        ll_btn8 = findViewById(R.id.ll_btn8);
        btn_update_pic.setOnClickListener(v -> {
            Glide.with(EditWordsActivity.this)
                    .load(edt_pic.getText().toString()).into(iv_img);
        });

        pickMedia = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
            if (uri != null) {
                edt_pic.setText(uri.toString());
                Glide.with(this).load(Uri.parse(uri.toString())).into(iv_img);
            } else {
                Log.d("PhotoPicker", "No media selected");
            }
        });

        iv_img.setOnClickListener(v -> {
            pickMedia.launch(new PickVisualMediaRequest.Builder()
                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                    .build());
        });

        tv_local_pic.setOnClickListener(v -> {
            pickMedia.launch(new PickVisualMediaRequest.Builder()
                    .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
                    .build());
        });

        if (id > 0) {
            List<WordBean> wordBeans = WordsDBDao.selectById(EditWordsActivity.this, id);
            if (wordBeans != null && wordBeans.size() != 0) {
                wordBean = wordBeans.get(0);
                if (TextUtils.isEmpty(wordBean.getImgUrl())) {
                    Glide.with(EditWordsActivity.this)
                            .load(wordBean.getImgLocal()).into(iv_img);
                } else {
                    Glide.with(EditWordsActivity.this)
                            .load(wordBean.getImgUrl()).into(iv_img);
                }
                edt_pic.setText(wordBean.getImgUrl());
                edt_introduce.setText(wordBean.getIntroduce());
                edt_means.setText(wordBean.getMeans());
                edt_short_sentence.setText(wordBean.getShortSentence());
                edt_sentence1.setText(wordBean.getSentence1());
                edt_sentence2.setText(wordBean.getSentence2());
                edt_sentence3.setText(wordBean.getSentence3());

                if (wordBean.getWordLevel() == WordBean.WORD_LEVEL.POST_GRADUATE) {
                    rg_word_level.check(R.id.rb_post_graduate);
                    ll_btn8.setVisibility(View.VISIBLE);
                } else if (wordBean.getWordLevel() == WordBean.WORD_LEVEL.COLLEGE_STUDENT) {
                    rg_word_level.check(R.id.rb_college_student);
                    ll_btn8.setVisibility(View.GONE);
                } else if (wordBean.getWordLevel() == WordBean.WORD_LEVEL.CET6) {
                    rg_word_level.check(R.id.rb_cet6);
                    ll_btn8.setVisibility(View.GONE);
                } else if (wordBean.getWordLevel() == WordBean.WORD_LEVEL.CET4) {
                    rg_word_level.check(R.id.rb_cet4);
                    ll_btn8.setVisibility(View.GONE);
                } else if (wordBean.getWordLevel() == WordBean.WORD_LEVEL.OTHER_LEVEL) {
                    rg_word_level.check(R.id.rb_other_level);
                    ll_btn8.setVisibility(View.GONE);
                }

                if (wordBean.getStudyType() == WordBean.STUDY_TYPE.WORDS) {
                    rg_study_type.check(R.id.rb_words);
                } else if (wordBean.getStudyType() == WordBean.STUDY_TYPE.WORD_ROOT) {
                    rg_study_type.check(R.id.rb_word_root);
                } else if (wordBean.getStudyType() == WordBean.STUDY_TYPE.PHRASES) {
                    rg_study_type.check(R.id.rb_phrases);
                } else if (wordBean.getStudyType() == WordBean.STUDY_TYPE.LINGUISTIC_CONTEXT) {
                    rg_study_type.check(R.id.rb_linguistic_context);
                } else if (wordBean.getStudyType() == WordBean.STUDY_TYPE.Example) {
                    rg_study_type.check(R.id.rb_example);
                } else if (wordBean.getStudyType() == WordBean.STUDY_TYPE.OTHER_TYPE) {
                    rg_study_type.check(R.id.rb_other_type);
                }
                btn_commit.setOnClickListener(v -> {
                    modifyWords();
                });
            }
        } else {
            // 因为默认情况下是研究生学习 所以默认打开学习类型选项
            ll_btn8.setVisibility(View.VISIBLE);
            btn_commit.setOnClickListener(v -> {
                addWords();
            });
        }

        rg_word_level.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_post_graduate) {
                    ll_btn8.setVisibility(View.VISIBLE);
                } else {
                    ll_btn8.setVisibility(View.GONE);
                }
            }
        });
    }

    public void addWords() {
        int word_level_check = rg_word_level.getCheckedRadioButtonId();
        WordBean.WORD_LEVEL wordLevel = null;
        if (word_level_check == R.id.rb_post_graduate) {
            wordLevel = WordBean.WORD_LEVEL.POST_GRADUATE;
        } else if (word_level_check == R.id.rb_college_student) {
            wordLevel = WordBean.WORD_LEVEL.COLLEGE_STUDENT;
        } else if (word_level_check == R.id.rb_cet6) {
            wordLevel = WordBean.WORD_LEVEL.CET6;
        } else if (word_level_check == R.id.rb_cet4) {
            wordLevel = WordBean.WORD_LEVEL.CET4;
        } else if (word_level_check == R.id.rb_other_level) {
            wordLevel = WordBean.WORD_LEVEL.OTHER_LEVEL;
        }

        int study_type_check = rg_study_type.getCheckedRadioButtonId();
        WordBean.STUDY_TYPE studyType = WordBean.STUDY_TYPE.NULL_TYPE;
        if (word_level_check == R.id.rb_post_graduate) {
            if (study_type_check == R.id.rb_other_type) {
                studyType = WordBean.STUDY_TYPE.OTHER_TYPE;
            } else if (study_type_check == R.id.rb_words) {
                studyType = WordBean.STUDY_TYPE.WORDS;
            } else if (study_type_check == R.id.rb_word_root) {
                studyType = WordBean.STUDY_TYPE.WORD_ROOT;
            } else if (study_type_check == R.id.rb_phrases) {
                studyType = WordBean.STUDY_TYPE.PHRASES;
            } else if (study_type_check == R.id.rb_linguistic_context) {
                studyType = WordBean.STUDY_TYPE.LINGUISTIC_CONTEXT;
            } else if (study_type_check == R.id.rb_example) {
                studyType = WordBean.STUDY_TYPE.Example;
            }
        }

        WordBean insert = WordsDBDao.insert(
                EditWordsActivity.this,
                edt_pic.getText().toString(),
                edt_introduce.getText().toString(),
                edt_means.getText().toString(),
                edt_short_sentence.getText().toString(),
                edt_sentence1.getText().toString(),
                edt_sentence2.getText().toString(),
                edt_sentence3.getText().toString(),
                wordLevel, studyType, -1);
        if (insert != null) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
        }
        finish();
    }

    public void modifyWords() {
        WordsDBDao.delete(EditWordsActivity.this, id);
        int word_level_check = rg_word_level.getCheckedRadioButtonId();
        WordBean.WORD_LEVEL wordLevel = null;
        if (word_level_check == R.id.rb_post_graduate) {
            wordLevel = WordBean.WORD_LEVEL.POST_GRADUATE;
        } else if (word_level_check == R.id.rb_college_student) {
            wordLevel = WordBean.WORD_LEVEL.COLLEGE_STUDENT;
        } else if (word_level_check == R.id.rb_cet6) {
            wordLevel = WordBean.WORD_LEVEL.CET6;
        } else if (word_level_check == R.id.rb_cet4) {
            wordLevel = WordBean.WORD_LEVEL.CET4;
        } else if (word_level_check == R.id.rb_other_level) {
            wordLevel = WordBean.WORD_LEVEL.OTHER_LEVEL;
        }

        int study_type_check = rg_study_type.getCheckedRadioButtonId();
        WordBean.STUDY_TYPE studyType = WordBean.STUDY_TYPE.NULL_TYPE;
        if (word_level_check == R.id.rb_post_graduate) {
            if (study_type_check == R.id.rb_other_type) {
                studyType = WordBean.STUDY_TYPE.OTHER_TYPE;
            } else if (study_type_check == R.id.rb_words) {
                studyType = WordBean.STUDY_TYPE.WORDS;
            } else if (study_type_check == R.id.rb_word_root) {
                studyType = WordBean.STUDY_TYPE.WORD_ROOT;
            } else if (study_type_check == R.id.rb_phrases) {
                studyType = WordBean.STUDY_TYPE.PHRASES;
            } else if (study_type_check == R.id.rb_linguistic_context) {
                studyType = WordBean.STUDY_TYPE.LINGUISTIC_CONTEXT;
            } else if (study_type_check == R.id.rb_example) {
                studyType = WordBean.STUDY_TYPE.Example;
            }
        }

        WordBean insert = WordsDBDao.insert(
                EditWordsActivity.this,
                id,
                TextUtils.isEmpty(edt_pic.getText().toString()) ? "" : edt_pic.getText().toString(),
                edt_introduce.getText().toString(),
                edt_means.getText().toString(),
                edt_short_sentence.getText().toString(),
                edt_sentence1.getText().toString(),
                edt_sentence2.getText().toString(),
                edt_sentence3.getText().toString(),
                wordLevel, studyType,
                wordBean.getImgLocal());
        if (insert != null) {
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        }
        finish();
    }
}